package com.comac.print.component.app.gatewayimpl.print;

import com.alibaba.fastjson.JSONObject;
import com.comac.mom.simpleintegration.mq.MQProducerComponent;
import com.comac.print.component.app.domain.gateway.enums.PtStatusEnum;
import com.comac.print.component.app.domain.gateway.print.PrintRecordGateway;
import com.comac.print.component.app.domain.gateway.printE.PrintRecordE;
import com.comac.print.component.app.dto.PrintResultDTO;
import com.comac.print.component.app.gatewayimpl.database.dataobject.PrintRecordDO;
import com.comac.print.component.app.gatewayimpl.database.dataobject.SMRelationDO;
import com.comac.print.component.app.gatewayimpl.database.dataobject.StatusDO;
import com.comac.print.component.app.gatewayimpl.database.mapper.PrintRecordMapper;
import com.comac.print.component.app.gatewayimpl.database.mapper.PrintStatusMapper;
import com.comac.print.component.app.gatewayimpl.database.mapper.SignMasterplateRelationMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;


import static cn.hutool.core.date.DateTime.now;


@Component
public class PrintRecordGatewayImpl implements PrintRecordGateway {

    @Autowired
    private PrintRecordMapper printRecordMapper;

    @Autowired
    private PrintStatusMapper printStatusMapper;

    @Autowired
    MQProducerComponent mqProducerComponent;

    @Autowired
    private SignMasterplateRelationMapper signMasterplateRelationMapper;

    @Override
    public void create(PrintRecordE printRecord) {
        //保存打印信息
        PrintRecordDO printRecordDO = new PrintRecordDO();
        BeanUtils.copyProperties(printRecord,printRecordDO);
        //printRecordDO.setCreateTime(now());
        //printRecordDO.setPrintTime(now());
        printRecordMapper.insert(printRecordDO);
        Integer printRecordId = printRecordDO.getId();
        //保存状态信息
        StatusDO statusDO = new StatusDO();
        statusDO.setPrintRecordId(printRecordId);
        /*if(StringUtils.isNotBlank(printRecord.getFailureReason())){
            statusDO.setStatus(PtStatusEnum.PT_FALSE.getCode());
            statusDO.setRemark("打印失败");
        }else {
            statusDO.setStatus(PtStatusEnum.PT_SUCCESS.getCode());
            statusDO.setRemark("打印成功");
        }*/
        statusDO.setCreateTime(now());
        //statusDO.setCreator(printRecord.getPrintPerson());
        statusDO.setIsNew(true);
        printStatusMapper.insert(statusDO);
        //保存关联信息
        SMRelationDO sMRelationDO = new SMRelationDO();
        sMRelationDO.setCreateTime(now());
        //sMRelationDO.setCreator(printRecord.getPrintPerson());
        //sMRelationDO.setStId(printRecord.getStId());
        //sMRelationDO.setMpId(printRecord.getMpId());
        signMasterplateRelationMapper.insert(sMRelationDO);
        //如果有消息订阅，将申请的结果推送到mq
        /*if (printRecord.getMessageSubscriptionFlag() == null) {
            printRecord.setMessageSubscriptionFlag(false);
        }
        if(printRecord.getMessageSubscriptionFlag()){
            SimpleDateFormat  formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            PrintResultDTO printResultDTO = new PrintResultDTO();
            printResultDTO.setPrintPerson(printRecord.getPrintPerson());
            printResultDTO.setPrintUrl(printRecord.getPrintUrl());
            printResultDTO.setPtId(printRecordId);
            printResultDTO.setFailureReason(printRecord.getFailureReason());
            PtStatusEnum ptStatusEnumByCode = PtStatusEnum.getPtStatusEnumByCode(statusDO.getStatus());
            printResultDTO.setStatus(ptStatusEnumByCode.getDescription());
            //打印时间
            printResultDTO.setPrintTime(formatter.format(new Date()));
            mqProducerComponent.sendMsg("PRINT-COMPONENT-RESULT", printRecord.getTag(), JSONObject.toJSON(printResultDTO).toString(),true);
        }*/
    }
}
